//
//  LGTMessageNoteVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/9/3.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit
import ZHRefresh

class LGTMessageNoteVC: LGTBaseVC, UIScrollViewDelegate{
    var line:UIImageView?
    var mainScrollView:UIScrollView?
    var buttonArray:Array<UIButton>?
    var vcArray:Array<LGTMessageListVC>?
    var currentVC:LGTMessageListVC?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.setInit()
    }
    
    override func setUI() {
        super.setUI()
        self.setBavBarStyle(navBarHiden: false, title: "消息通知", back: true)
        let array = ["消息中心", "系统公告"]
        let count = array.count
        let w = KScreenW / CGFloat(count)
        let h = SizeFit(44)
        self.buttonArray = []
        self.vcArray = []
        
        let scrollY = NavBarH + SizeFit(44)
        let scrollH = Double(KScreenH) - scrollY
        let scrollW = Double(KScreenW)
        self.mainScrollView = UIScrollView.init(frame: CGRect.init(x: 0.0, y: scrollY, width: scrollW, height: scrollH))
        self.view.addSubview(self.mainScrollView!)
        self.mainScrollView?.isPagingEnabled = true
        self.mainScrollView?.delegate = self
        self.mainScrollView?.showsHorizontalScrollIndicator = false
        self.mainScrollView?.contentSize = CGSize.init(width: scrollW * Double(count), height: scrollH)
        self.mainScrollView?.backgroundColor = ViewBackColor
        for i in 0..<count{
            let childVC:LGTMessageListVC = LGTMessageListVC()
            childVC.type = i
            //childVC.orderType = i+1
            let frame = CGRect.init(x: scrollW * Double(i), y: 0.0, width: scrollW, height: scrollH)
            childVC.view.frame = frame
            //childVC.updateFrame(frame: frame)
            // childVC.view.frame = CGRect.init(x: scrollW * Double(i), y: 0.0, width: scrollW, height: scrollH)
            // self.mainScrollView?.addSubview(childVC.view)
            vcArray?.append(childVC)
            
            let title:String = array[i]
            let button:UIButton = LGTViewCreateUtil.share.createTextLGTButton(title: title, font: Font_Normal_18, color: TextColor, selectedColor: MainTintColor)
            self.buttonArray?.append(button)
            button.backgroundColor = UIColor.white
            self.view.addSubview(button)
            button.tag = i
            button.addTarget(self, action: #selector(clickAction), for: UIControl.Event.touchUpInside)
            
            self.view.addSubview(button)
            button.snp.makeConstraints { (make) in
                make.left.equalTo(CGFloat(i) * w)
                make.height.equalTo(h)
                make.top.equalTo(NavBarH)
                make.width.equalTo(w)
            }
        }
        
        let line:UIImageView = LGTViewCreateUtil.share.createLineView()
        self.view.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(12))
            make.right.equalTo(-SizeFit(12))
            make.height.equalTo(1)
            make.top.equalTo(NavBarH + SizeFit(43))
        }
        
        self.line = LGTViewCreateUtil.share.createImageViewCorner(corner: 1)
        self.line?.frame = CGRect.init(x: Double((w - SizeFitFloat(48))/2), y: NavBarH + SizeFit(41), width: SizeFit(48), height: 2)
        self.view.addSubview(self.line!)
        self.line?.backgroundColor = MainTintColor
    }
    
    func setInit(){
        self.clickAction(sender: self.buttonArray![0])
    }
    
    @objc func clickAction(sender:UIButton){
        let index = sender.tag
        var frame = self.line?.frame
        var orgin = frame?.origin
        let w = KScreenW / CGFloat(self.buttonArray?.count ?? 1)
        orgin?.x =  (w - SizeFitFloat(48)) / 2 + CGFloat(index) * w
        frame?.origin = orgin!
        self.line!.frame = frame!
        let count = self.buttonArray!.count
        for i in 0..<count{
            let button:UIButton = self.buttonArray![i]
            button.isSelected = false
            if i == index{
                button.isSelected = true
            }
        }
        
        let currentVC:LGTMessageListVC = (self.vcArray?[index])!
        if currentVC == self.currentVC{
            return
        }
        
        self.mainScrollView?.contentOffset = CGPoint.init(x: self.view.frame.size.width * CGFloat(index), y: 0)
        if self.currentVC == nil{
            self.currentVC = currentVC
            self.addChild(self.currentVC!)
            self.currentVC!.beginAppearanceTransition(true, animated: false)
            self.mainScrollView?.addSubview((self.currentVC!.view)!)
            self.currentVC!.willMove(toParent: self)
        }else{
            self.addChild(currentVC)
            weak var weakSelf = self
            self.transition(from: self.currentVC!, to: currentVC, duration: 0, options: UIView.AnimationOptions.transitionCrossDissolve, animations: {
                
            }) { (finish) in
                if finish == true{
                    currentVC.didMove(toParent: self)
                    weakSelf?.mainScrollView?.addSubview(currentVC.view)
                    weakSelf?.currentVC?.willMove(toParent: nil)
                    weakSelf?.currentVC?.view.removeFromSuperview()
                    weakSelf?.currentVC?.removeFromParent()
                    weakSelf?.currentVC = currentVC
                }else{
                    weakSelf?.currentVC = currentVC
                }
            }
        }
        
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x / self.view.frame.size.width)
        self.clickAction(sender: self.buttonArray![index] as UIButton)
    }
}
